<?php

/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/7/8
 * Time: 12:27
 */
class SmsAction extends BaseAction
{

    function __construct()
    {
        $this->no_login=['index','login'];
        parent::__construct();
    }
    function index(){
        $code = Req::post('code');
        $mobile = Req::post('mobile');
        $Sms = new sms();
        if (strlen($code) != 4 || !vcode::check($code)) {
            return $this->result(1,'验证码不对');
        }
        if ($mobile == '' || !is_numeric($mobile)) {
            return $this->result( 2,'手机号码不对');
        }
        if ($mobile == "" || !is_numeric($mobile)) {
            return($this->result(1,'请输入常用的手机号码'));
        }
        $TodayMsgCount = $Sms->count($mobile);
        if ($TodayMsgCount >= 5) {
            die($this->result(1,'您今天发送的验证码次数已用完，请明天再试,有疑问请联系客服!'));
        }
        if($Sms->is_has_usr($mobile)){
            //return $this->result(1,'已经注册过了,如果忘记了,请找回');
        }
        if ($Sms->send($mobile)) {
            die($this->result(0,'发送成功'));
        } else {
            die($this->result(1,'短信未能发出，请稍后再试!'));
        }

    }
    /* 发送手机验证码
   *
   */
    function ValidCode($mobile)
    {
        $table = 'validcodelog';
        $result = array('status' => '', 'msg' => '');
        $TodayMsgCount = Db::table('validcodelog')->where('code_type = 0 and state = 0 and account = \'' . $mobile . '\' and to_days(add_time) = to_days(now())')->count();
        if ($TodayMsgCount >= 5) {
            $result['status'] = "err";
            $result['msg'] = "您今天发送的验证码次数已用完，请明天再试,有疑问请联系客服!";
            return $result;
        }
        $validcode = rand(1000, 9999);
        $SMS = "您的验证码是：{$validcode}。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        Session::set('vercode',$validcode);
        $data = [
            'userid' => 0,
            'code_type' => 0,
            'account' => $mobile,
            'content' => $SMS,
            'add_time' => date('Y-m-d H:i:s'),
            'state' => 0
        ];
        Db::table($table)->insert($data);
        $insertID = Db::getLastInsID();
        //发送短信
        $smsReturn = $this->PostSMS($mobile,$SMS);
        $smsReturn = 'ok';
        if ($smsReturn == "ok") {
            $result['status'] = "ok";
            $result['msg'] = "发送成功";
            $_SESSION['mobilesmscode'] = $validcode;
        } else {
            $result['status'] = "err";
            $result['msg'] = "短信未能发出，请稍后再试!";
            Db::table($table)->update(['state' => 1, 'err_msg' => $result['msg'], 'id' => $insertID]);
        }

        return $result;
    }

    /* 取得绑定/解绑手机信息
    *
    */
    function GetBindMobileInfo()
    {
        global $db;
        $BindType = $_GET['t'];

        $sql = "select id,mobile from users where id = '{$_SESSION['usersid']}'";
        $result = $db->query($sql);
        if ($rs = $db->fetch_array($result)) {
            $Mobile = $rs['mobile'];
        }
        if (isset($_SESSION['mobilecodecount']))
            unset($_SESSION['mobilecodecount']);
        $RetContent = "<div class='popup'>\r\n";
        //header
        $RetContent .= "\t<div class='popup-header'>\r\n";
        $RetContent .= "\t\t\t<h2>绑定/解绑手机</h2>\r\n";
        $RetContent .= "\t\t\t<a href='javascript:;' onclick='closerecord(-1)' title='关闭' class='close-link'>[关闭]</a>\r\n";
        $RetContent .= "\t\t\t<br clear='both' /> \r\n";
        $RetContent .= "\t</div>";
        //body
        $RetContent .= "\t<div class='popup-body'>\r\n";
        $RetContent .= "\t\t<div class='table'>\r\n";
        $RetContent .= "\t\t\t<table class='table_list' cellspacing='0px' style='border-collapse:collapse;border:1px;width:500;height:400;'>\r\n";
        if ($BindType == "bind")
            $RetContent .= "\t\t\t\t<tr><td width=100>手机</td><td width=350 style='text-align:left;'><input id='txtBindMobile' value='{$Mobile}' maxlength=11 >11位手机号码</td>";
        else
            $RetContent .= "\t\t\t\t<tr><td width=100>手机</td><td width=350 style='text-align:left;'><input id='txtBindMobile' value='{$Mobile}' maxlength=11 disabled='disabled'></td>";
        $RetContent .= "\t\t\t\t<tr><td>验证码</td><td style='text-align:left;'><input type='text' style='width:50px' id='txtMobileValidCode' maxlength=4><input type='button' id='btnGetMobileValid' style='width:200' value='获取验证码' /><label id='lblBindtype' style='display:none'>{$BindType}</label></td></tr>\r\n";
        if ($BindType == "bind")
            $RetContent .= "\t\t\t\t<tr><td></td><td><input type='button' id='btnBindMobile' style='width:100' value='马上绑定' /></td></tr>\r\n";
        else
            $RetContent .= "\t\t\t\t<tr><td></td><td><input type='button' id='btnUnBindMobile' style='width:100' value='解绑' /></td></tr>\r\n";

        $RetContent .= "\t\t\t</table>\r\n";

        $RetContent .= "\t\t</div>\r\n";
        $RetContent .= "\t</div>";

        echo $RetContent;
        exit;

    }

    function PostSMS($mobile, $content)
    {
        $target = "http://sms.106jiekou.com/utf8/sms.aspx";
        //替换成自己的测试账号,参数顺序和wenservice对应
        $post_data = "account=robinson&password=qq52013145678&mobile={$mobile}&content=" . rawurlencode($content);

        $smsResult = $this->Post($post_data, $target);
        //$xml = simplexml_load_string($smsResult);
        //$result = (string) $xml->result;
        $ret = "ok";
        switch ($smsResult) {
            case "100":
                $ret = "ok";
                break;
            case "101":
                $ret = "验证失败";
                break;
            case "102":
                $ret = "手机号码格式不正确";
                break;
            case "103":
                $ret = "会员级别不够";
                break;
            case "104":
                $ret = "内容未审核";
                break;
            case "105":
                $ret = "内容过多";
                break;
            case "106":
                $ret = "账户余额不足";
                break;
            case "107":
                $ret = "Ip受限";
                break;
            case "108":
                $ret = "手机号码发送太频繁，请换号或隔天再发";
                break;
            case "109":
                $ret = "帐号被锁定";
                break;
            case "110":
                $ret = "发送通道不正确";
                break;
            case "111":
                $ret = "当前时间段禁止短信发送";
                break;
            case "120":
                $ret = "系统升级";
                break;
            default:
                $ret = "未知错误:" . $smsResult;
                break;
        }
        return $ret;
    }


}